package com.lsa.letcode.the20.validParenth;

import java.util.Stack;

public class Solution {
	static char[] map = new char[128];
	{
    	map[')'] = '(';
    	map[']'] = '[';
    	map['}'] = '{';
	}
	
    public boolean isValid(String s) {
    	Stack<Character> stack = new Stack<Character>();
    	for (int i = 0; i < s.length(); i++) {
    		char c = s.charAt(i);
    		switch(c) {
    		case '(':
    		case '{':
    		case '[':
    			stack.push(c);
    		break;
    		case ')':
    		case ']':
    		case '}':
    			if (stack.empty()) {
    				return false;
    			}
    			char c2 = map[c];
    			if (stack.pop() != c2) {
    				return false;
    			}
    		break;
    		default:
    			return false;
    		}
		}
    	
        return stack.isEmpty();
    }
}